package com.ycw.service.guoguo.impl;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ycw.bean.Comments;
import com.ycw.bean.Funding;
import com.ycw.bean.Letter;
import com.ycw.bean.Projects;
import com.ycw.bean.Users;
import com.ycw.dao.CommentsMapper;
import com.ycw.dao.FundingMapper;
import com.ycw.dao.LetterMapper;
import com.ycw.dao.ProjectsMapper;
import com.ycw.dao.ProjectsTypeMapper;
import com.ycw.dao.UsersMapper;
import com.ycw.service.guoguo.PersonalCenterService;
import com.ycw.util.Msg;

/**
 * 个人中心功能实现类
 * @author gcz
 *
 */
@Service
public class PersonalCenterServiceImpl implements PersonalCenterService {

	@Autowired
	private ProjectsMapper projectsMapper;
	@Autowired
	private FundingMapper fundingMapper;
	@Autowired
	private ProjectsTypeMapper projectsTypeMapper;
	@Autowired
	private CommentsMapper commentsMapper;
	@Autowired
	private LetterMapper letterMapper;
	@Autowired
	private UsersMapper usersMapper;
	
	@Override
	public Msg getMyInitiateProjects(Integer pageNum, HttpSession httpSession) {
		
		
		Users users = (Users)httpSession.getAttribute("users");
		
		Projects projects = new Projects();
		projects.setPsUsId(users.getUsId());
		
		PageHelper.startPage(pageNum, 10);
		PageInfo<Projects> pageInfo = new PageInfo<>(projectsMapper.select(projects));
		PageHelper.clearPage();
		
		
		return Msg.success().add("pageInfo", pageInfo).add("projectsType", projectsTypeMapper.selectAll());
	}

	@Override
	public Msg getMyParticipateProjects(HttpSession httpSession) {
		Users users = (Users)httpSession.getAttribute("users");
		
		Funding funding = new Funding();
		funding.setFdUsId(users.getUsId());

		List<Funding> list = fundingMapper.getMyParticipateProjects(funding);
		return Msg.success().add("list", list);
	}

	@Override
	public Msg getMyComments(HttpSession httpSession) {
		Users users = (Users)httpSession.getAttribute("users");
		
		Comments comments = new Comments();
		comments.setCmUsId(users.getUsId());
		
		return Msg.success().add("list", commentsMapper.select(comments));
	}

	@Override
	public Msg getMyletter(HttpSession httpSession) {
		Users users = (Users)httpSession.getAttribute("users");
		
		Letter letter = new Letter();
		letter.setLeUsAddresserid(users.getUsId());
		
		return Msg.success().add("list", letterMapper.select(letter));
	}

	@Override
	public Msg getMyUsers(HttpSession httpSession) {
		Users users = (Users)httpSession.getAttribute("users");
		return Msg.success().add("users", users);
	}

	@Override
	public Msg alertMyPassword(HttpSession httpSession, Users users) {
		Users Myusers = (Users)httpSession.getAttribute("users");
		
		if(users.getUsPassword().equals(Myusers.getUsPassword())){
			return Msg.fail();
		}
		users.setUsId(Myusers.getUsId());
		
		if(usersMapper.updateByPrimaryKeySelective(users)>0){
			return Msg.success();
		}
		return Msg.fail();
	}

}
